;
(function() {
	var role = require("./role");
	var ResponObj = require("../_include/responObj");
	var db = require("../../core/db/index");
	var config = require("../../core/_include/config");
	//自动把地址数据加入到数据库中
	if (!db.find_all(config.dbname.city).length) {
		var city_list = require("../_include/city/city.json");
		city_list.forEach(function(item) {
			db.insert(config.dbname.city, item, item.region_id);
		});
	}
	module.exports = {
		prefix: "/city",
		get: {
			//获取三级城市列表
			"/by_parent_id/:parent_id": function() {
				var parent_id = this.params.parent_id;
				var list = db.find_list(config.dbname.city, {
					"parent_id": parent_id
				});
				this.body = ResponObj("json", list);
			},
			"/by_name/:parent_name": function() {
				var city_name = this.params.parent_name;
				var region_type = this.request.query.region_type;
				var search_query = {
					region_name: city_name
				};
				if (region_type) {
					search_query.region_type = region_type;
				}
				var parent = db.find_one(config.dbname.city, search_query);
				var list = db.find_list(config.dbname.city, {
					"parent_id": parent.region_id
				});
				this.body = ResponObj("json", list);
			}
		}
	}
}());